<template>
  <div @keydown.enter.prevent="handleSubmit" class="login-container">
    <!-- 系统介绍 -->
    <div class="system-introduction lg">
      <div class="introduction-wrapper">
        <div class="login-background"></div>
        <div class="login-center">
          <SloganIcon class="slogan" />
          <div class="title lg:text-2xl"></div>
          <div class="subtitle"></div>
        </div>
      </div>
    </div>
    <!-- 右侧认证面板 -->
    <div class="authentication-panel lg:px-8 lg:flex-initial">
      <div class="panel-wrapper md:max-w-md sm:mx-auto">
        <div class="text sm:mx-auto sm:w-full sm:max-w-md">
          <h2 class="lg:text-4xl">
            {{ title }}
          </h2>
          <p class="lg:text-md text-sm">
            <span>{{ subTitle }}</span>
          </p>
        </div>
        <el-form :model="form" :rules="rules" ref="formRef" size="large">
          <el-form-item prop="username">
            <el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model.trim="form.password"
              placeholder="密码"
              type="password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item prop="isVerifyPass">
            <div class="el-input__wrapper">
              <DragVerify v-model:value="form.isVerifyPass" />
            </div>
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="form.rememberMe">记住账号</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleSubmit" type="primary" style="width: 100%">登录</el-button>
          </el-form-item>
        </el-form>
        <!-- <div style="float: right">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div> -->
      </div>
    </div>
  </div>
</template>

<script setup>
import SloganIcon from '@/views/login/SloganIcon.vue'
import DragVerify from '@/views/login/DragVerify.vue'
import { encrypt, decrypt } from '@/utils/jsencrypt.js'
import { dynamicRoutes } from '@/router/index.js'
import { useAuthStore } from '@/stores/auth.js'
import { userLogin } from '@/api/user.js'
import { useRouter } from 'vue-router'
import CryptoJS from 'crypto-js'
import Cookies from 'js-cookie'

const form = ref({
  username: '',
  password: '',
  rememberMe: false,
  isVerifyPass: '',
})
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  isVerifyPass: [
    {
      required: true,
      message: '请拖动滑块完成验证',
      trigger: 'blur',
    },
  ],
}
const formRef = ref()
const dragVerifyRef = ref()
const props = defineProps({
  forgetPasswordPath: {
    type: String,
    default: '',
  },
  showRememberMe: {
    type: Boolean,
    default: true,
  },
  showForgetPassword: {
    type: Boolean,
    default: true,
  },
  title: {
    type: String,
    default: '欢迎回来 👋🏻',
  },
  subTitle: {
    type: String,
    default: '请输入您的帐户信息以开始管理您的项目',
  },
  loading: {
    type: Boolean,
    default: false,
  },
  formSchema: {
    type: Array,
    default: () => [],
  },
})
const isError = ref(false)
const REMEMBER_ME_KEY = `REMEMBER_ME_USERNAME_${location.hostname}`
const localUsername = localStorage.getItem(REMEMBER_ME_KEY) || ''
const rememberMe = ref(!!localUsername)

const authStore = useAuthStore()
const router = useRouter()

watch(
  () => form.value.isVerifyPass,
  () => {
    formRef.value.validateField('isVerifyPass')
  },
)

function handleSubmit() {
  const data = {
    username: form.value.username,
    password: form.value.password,
  }
  formRef.value.validate((valid) => {
    if (valid) {
      if (form.value.rememberMe) {
        Cookies.set('username', form.value.username, { expires: 30 })
        Cookies.set('password', encrypt(form.value.password))
        Cookies.set('rememberMe', encrypt(form.value.rememberMe))
      } else {
        Cookies.remove('username')
        Cookies.remove('password')
        Cookies.remove('rememberMe')
      }
      authStore.setToken('')
      userLogin(data).then(async (res) => {
        const data = res.data
        data.menus = dynamicRoutes
        await authStore.setLoginUserInfo(data)
        const firstRoute = authStore.getFirstRoute()
        router.replace(firstRoute.fullPath)
      })
    }
  })
}

function getCookie() {
  const username = Cookies.get('username')
  const password = Cookies.get('password')
  const rememberMe = Cookies.get('rememberMe')
  form.value = {
    username: username === undefined ? form.value.username : username,
    password: password === undefined ? form.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(decrypt(rememberMe)),
    isVerifyPass: '',
  }
}

function handleGo() {}

onMounted(() => {
  getCookie()
})
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  overflow-x: hidden;
  flex: 1 1 0;
  min-height: 100%;
  user-select: none;

  // 系统介绍
  .system-introduction {
    position: relative;
    width: 0;
    display: none;
    flex: 1 1 0;

    .introduction-wrapper {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: hsl(216 20.11% 95.47%);

      .login-background {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: transparent;
        background: linear-gradient(
          154deg,
          rgba(7, 7, 9, 0.08235) 30%,
          hsl(212 100% 45% / 30%) 48%,
          rgba(7, 7, 9, 0.08235) 64%
        );
        filter: blur(100px);
      }

      .login-center {
        height: 100%;
        margin-right: 5rem;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;

        .slogan {
          animation: float 5s linear 0ms infinite;
          width: 40%;
          height: 16rem;

          img,
          svg,
          video,
          canvas,
          audio,
          iframe,
          embed,
          object {
            display: block;
          }
        }

        .title {
          color: hsl(210 6% 21%);
          font-family:
            -apple-system, blinkmacsystemfont, 'Segoe UI', roboto, 'Helvetica Neue', arial,
            'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
            'Noto Color Emoji';
          margin-top: 1.5rem;
        }

        .subtitle {
          margin-top: 0.5rem;
        }

        @keyframes float {
          0% {
            transform: translateY(0);
          }
          50% {
            transform: translateY(-20px);
          }

          100% {
            transform: translateY(0);
          }
        }
      }
    }
  }

  // 认证面板
  .authentication-panel {
    padding: 2.5rem 1.5rem;
    // background-color: hsl(0 0 100%);
    flex: 1 1 0;
    width: 34%;
    min-height: 100%;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .panel-wrapper {
      width: 100%;
      margin-top: 1.5rem;

      .text {
        margin-bottom: 1.75rem;

        h2 {
          color: hsl(210 6% 21%);
          letter-spacing: -0.025em;
          line-height: 2.25rem;
          font-weight: 700;
          font-size: 1.875rem;
          margin-bottom: 0.75rem;
        }

        p {
          color: hsl(240 3.8% 46.1%);
          font-size: 0.875rem;
          line-height: 1.25rem;

          span {
            color: hsl(240 3.8% 46.1%);
          }
        }
      }

      .el-input__wrapper {
        padding: 1px;
      }
    }
  }
}

@media (min-width: 1024px) {
  .lg {
    display: block !important;
  }
  .lg\:px-8 {
    padding-left: 2rem !important;
    padding-right: 2rem !important;
  }
  .lg\:flex-initial {
    flex: 0 1 auto !important;
  }
  .lg\:text-2xl {
    font-size: 1.5rem;
    line-height: 2rem;
  }
  .lg\:text-4xl {
    font-size: 2.25rem;
    line-height: 2.5rem;
  }
}

@media (min-width: 768px) {
  .md\:max-w-md {
    max-width: 28rem;
  }
}

@media (min-width: 640px) {
  .sm\:mx-auto {
    margin-left: auto;
    margin-right: auto;
  }
  .sm\:max-w-md {
    max-width: 28rem;
  }
  .sm\:w-full {
    width: 100%;
  }
  .sm\:mx-auto {
    margin-left: auto;
    margin-right: auto;
  }
}
</style>
